<template>
  <div class="mod-activityList">
    <avue-crud
      ref="crudRef"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="onSearch"
      @on-load="getDataList"
      @refresh-change="refreshChange"
      @selection-change="selectionChange"
    >
      <template #menu-left>
        <el-button
          v-if="isAuth('activity:list:save')"
          type="primary"
          icon="el-icon-plus"
          @click="onAddOrUpdate()"
        >
          新增
        </el-button>
      </template>

      <template #status="scope">
        <el-tag
          v-if="scope.row.status === 0"
          type="danger"
        >
          未开始
        </el-tag>
        <el-tag
          v-else-if="scope.row.status === 1"
          type="success"
        >
          进行中
        </el-tag>
        <el-tag
          v-else-if="scope.row.status === 2"
          type="info"
        >
          已结束
        </el-tag>
        <el-tag
          v-else
          type="info"
        >
          草稿
        </el-tag>
      </template>

      <template #menu="scope">
        <el-button
          v-if="isAuth('activity:list:approval') && (scope.row.status === 3)"
          type="warning"
          @click="onApproval(scope.row)"
        >
          发布
        </el-button>
        <el-button
          v-if="isAuth('activity:list:update') && scope.row.status === 3"
          type="primary"
          @click="onAddOrUpdate(scope.row.id)"
        >
          修改
        </el-button>
        <el-button
          v-if="isAuth('activity:list:delete') && scope.row.status === 3"
          type="danger"
          @click="onDelete(scope.row.id)"
        >
          删除
        </el-button>
        <el-button
          v-if="isAuth('activity:list:approval') && scope.row.status < 3"
          type="warning"
          @click="onTakedown(scope.row.id)"
        >
          下架
        </el-button>
        <el-button
          v-if="scope.row.status < 3"
          type="primary"
          @click="onActivityEnrollments(scope.row.id)"
        >
          活动参与者
        </el-button>
      </template>
    </avue-crud>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="getDataList"
    />
    <!--参与用户信息-->
    <activityEnrollments
      v-if="activityEnrollmentsVisible"
      ref="activityEnrollmentsRef"
      @refresh-data-list="getDataList"
    />
  </div>
</template>

<script setup>
import {isAuth} from '@/utils'
import {ElMessage, ElMessageBox} from 'element-plus'
import {tableOption} from '@/crud/activity/list.js'
import AddOrUpdate from './add-or-update.vue'
import ActivityEnrollments from './activityEnrollments.vue'

const dataList = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dataListLoading = ref(false)
/**
 * 获取数据列表
 */
const getDataList = (pageParam, params, done) => {
  dataListLoading.value = true
  http({
    url: http.adornUrl('/admin/activity/page'),
    method: 'get',
    params: http.adornParams(Object.assign({
      orderBy: 0,
      current: pageParam ? pageParam.currentPage : 1,
      size: pageParam ? pageParam.pageSize : 20
    }, params))
  })
    .then(({data}) => {
      page.total = data.total
      page.pageSize = data.size
      page.currentPage = data.current
      dataList.value = data.records
      dataListLoading.value = false
      if (done) done()
    })
}
const dataListSelections = ref([])
/**
 * 多选回调
 * @param list
 */
const selectionChange = (list) => {
  dataListSelections.value = list
}

const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)
/**
 * 新增 / 修改
 * @param id
 */
const onAddOrUpdate = (id) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}
const activityEnrollmentsVisible = ref(false)
const activityEnrollmentsRef = ref(null)

const onActivityEnrollments = (id) => {
  activityEnrollmentsVisible.value = true
  nextTick(() => {
    activityEnrollmentsRef.value?.init(id)
  })
}
/**
 * 点击查询
 */
const onSearch = (params, done) => {
  getDataList(page, params, done)
}

/**
 * 发布
 * @param id
 */
const onApproval = (row) => {
  ElMessageBox.confirm(`确定进行发布"操作?若当前时间处于活动开始与结束时间内，则活动将直接进行。`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      http({
        url: http.adornUrl(`/admin/activity/approval`),
        method: 'post',
        data: http.adornData({
          id: row.id,
          startTime: row.startTime,
          endTime: row.endTime,
          status: 0
        })
      })
        .then(() => {
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              getDataList()
            }
          })
        })
    })
}

/**
 * 下架
 */
const onTakedown = (id) => {
  ElMessageBox.confirm('确定进行下架操作?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      http({
        url: http.adornUrl(`/admin/activity/takedown/${id}`),
        method: 'post'
      })
        .then(() => {
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              getDataList()
            }
          })
        })
    })
}
/**
 * 删除
 */
const onDelete = (id) => {
  ElMessageBox.confirm('确定进行删除操作?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      http({
        url: http.adornUrl(`/admin/activity/del/${id}`),
        method: 'post'
      })
        .then(() => {
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              getDataList()
            }
          })
        })
    })
}
const refreshChange = () => {
  getDataList(page)
}

</script>
